package app.itgungnir.compose.wan.business.user_square

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Link
import androidx.compose.material.icons.filled.Title
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.core.text.HtmlCompat
import app.itgungnir.compose.wan.business.main.square.Square
import app.itgungnir.compose.wan.widgets.*

@Composable
fun UserSquareItem(item: Square?, onClick: (Square) -> Unit = {}) {
    item ?: return
    WanCardRow(onClick = { onClick.invoke(item) }) {
        SquareItemContent(item)
    }
}

@Composable
fun MySquareItem(item: Square?, onClick: (Square) -> Unit = {}, onDelete: (Square) -> Unit = {}) {
    item ?: return
    WanSwipeMenuCard(
        modifier = Modifier.padding(top = 10.dp),
        menuItems = listOf(
            WanSwipeMenuItem(icon = Icons.Filled.Delete, title = "删除", backgroundColor = MaterialTheme.colors.error) {
                onDelete.invoke(item)
            }
        ),
        onClick = { onClick.invoke(item) }
    ) {
        SquareItemContent(item)
    }
}

@Composable
private fun SquareItemContent(item: Square) {
    Column(modifier = Modifier.fillMaxWidth()) {
        WanTitle(content = HtmlCompat.fromHtml("${item.title}", HtmlCompat.FROM_HTML_MODE_COMPACT).toString())
        Spacer(modifier = Modifier.height(3.dp))
        WanInfo(modifier = Modifier.align(Alignment.End), content = item.niceShareDate)
    }
}

@Composable
fun UserSquareDialogUI(callback: (String, String) -> Unit) {
    val focusManager = LocalFocusManager.current
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(color = MaterialTheme.colors.background, shape = RoundedCornerShape(size = 12.dp))
            .padding(all = 15.dp)
    ) {
        val titleValue = remember { mutableStateOf(TextFieldValue(text = "")) }
        val linkValue = remember { mutableStateOf(TextFieldValue(text = "")) }
        WanTitle(
            modifier = Modifier.align(Alignment.CenterHorizontally),
            content = "新增广场文章"
        )
        Spacer(modifier = Modifier.height(5.dp))
        WanOutlinedInput(state = titleValue, label = "标题", icon = Icons.Filled.Title)
        Spacer(modifier = Modifier.height(5.dp))
        WanOutlinedInput(state = linkValue, label = "链接", icon = Icons.Filled.Link)
        Spacer(modifier = Modifier.height(20.dp))
        WanButton(modifier = Modifier.fillMaxWidth(), text = "确认新增") {
            focusManager.clearFocus()
            callback.invoke(titleValue.value.text, linkValue.value.text)
        }
    }
}
